import {
  init,
  webList,
  getNextClassifications,
  add,
  move,
  edit,
  deleteClassification,
  actionDelete,
  classificationWebAddOrModify,
  webSortDoing,
  webMoveDoing,
  queryShowroomList, modifyShowroom
} from './classification_funs'
import tiyData from "./classification_data";
import tiyDataImage from "../../../image/js/select/image_data"
import {
  imageInit,
  webImageList,
} from "../../../image/js/select/image_funs"

/**
 * 分类页面的computed方法
 */
export const VueComputed = {
  authAdd() {
    return this.$authority('PRODUCT-2-200')
  },
  authSort() {
    return this.$authority('PRODUCT-2-201')
  },
  authModify() {
    return this.$authority('PRODUCT-2-203')
  },
  authDelete() {
    return this.$authority('PRODUCT-2-204')
  },
  authMove() {
    return this.$authority('PRODUCT-2-205')
  },
  classificationTypeDict(){
    return this.$store.getters.dictionaries.classificationType
  },

};

export const VueMethods = {
  toggle(classification, index) {
    switch (classification.level) {
      case 1:
        tiyData.status.setSelectedLevel1(classification, index);
        tiyData.status.resetSelectedLevel(2);
        tiyData.status.selectedLevel2.index = -1;
        tiyData.status.selectedLevel3.index = -1;
        getNextClassifications(this, tiyData, classification.level + 1);
        break;
      case 2:
        tiyData.status.setSelectedLevel2(classification, index);
        tiyData.status.resetSelectedLevel(3);
        tiyData.status.selectedLevel3.index = -1;
        getNextClassifications(this, tiyData, classification.level + 1);
        break;
      case 3:
        tiyData.status.setSelectedLevel3(classification, index);
        break;
      default :
        break;
    }
  },
  classificationAdd(level) {
    add(this, tiyData, level);
  },
  classificationMove(level, index) {
    move(tiyData, index, level);
  },
  classificationEdit(id) {
    edit(this, tiyData, id);
  },
  classificationDelete(id, level, name) {
    tiyData.status.setDeleteItem(id, level, name);
    deleteClassification(this, tiyData);
  },
  actionDelete() {
    actionDelete(this, tiyData);
  },
  classificationWebAddOrModify() {
    this.trimShortLinkName();
    classificationWebAddOrModify(this, tiyData, 'classificationSimple');
  },
  selectImages(showChooseSingle, isSingle, folderType, deviceType) {//type 0:橱窗图 单   1:色卡样式图 单    2:试戴图单    3:明细图上传 多
    imageInit(this, tiyDataImage, showChooseSingle, isSingle, folderType);
    tiyData.addModal.modalImageSelect.isShow = true;
    tiyData.addModal.modalImageSelect.deviceType = deviceType;
    webImageList(this, tiyDataImage);
  },
  /**
   * 选择分类图标
   * @param selectedImages
   */
  seletedImage(selectedImages) {
    this.addModal.modalImageSelect.isShow = false;
    if (tiyDataImage.tStatus.isSingle && 0 < selectedImages.length) {
      switch(this.addModal.modalImageSelect.deviceType){//1：pc图标；2：手机端图标
        case 1:
          tiyData.tParam.classification.icon = selectedImages[0].yunKey;
          break;
        case 2:
          tiyData.tParam.classification.mobileIcon = selectedImages[0].yunKey;
          break;
        default:
          break;
      }
    }
  },
  copy(level) {
    var oInput = document.createElement('input');
    var shortName;
    if (level == 1) {
      shortName = this.classificationList[this.status.selectedLevel1.index].shortLinkUrl;
    } else if (level == 2) {
      shortName = this.secondClassificationList[this.status.selectedLevel2.index].shortLinkUrl;
    } else {
      shortName = this.thirdClassificationList[this.status.selectedLevel3.index].shortLinkUrl;
    }
    oInput.value = shortName;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    oInput.className = 'oInput';
    oInput.style.display = 'none';
    this.$Message.success("短连接 " + shortName + ' 复制成功');
  },
  trimShortLinkName() { //短链接里的空格替换为-
    tiyData.tParam.classification.shortLinkName = tiyData.tParam.classification.shortLinkName.replaceAll(" ", "-").toLowerCase();
    tiyData.tParam.classification.shortLinkUrl = tiyData.tParam.classification.shortLinkName + ".html";
  },
  sortClassification(level){
    let leveClassification = [];
    let levelTitle = "一级分类";

    switch (level) {
      case 1:
        levelTitle = "一级分类";
        tiyData.tData.sortData.sortName = "一级";
        leveClassification = tiyData.tData.classificationList;
        break;
      case 2:
        levelTitle = "二级分类";
        tiyData.tData.sortData.sortName = tiyData.status.selectedLevel1.name;
        leveClassification = tiyData.tData.secondClassificationList;
        break;
      case 3:
        levelTitle = "三级分类";
        tiyData.tData.sortData.sortName = tiyData.status.selectedLevel2.name;
        leveClassification = tiyData.tData.thirdClassificationList;
        break;
      default:
        break;
    }
    tiyData.tData.sortData.sortList.length = 0;
    leveClassification.forEach(function (item) {
      tiyData.tData.sortData.sortList.push(item);
    });
    if(tiyData.tData.sortData.sortList.length == 0){
      this.$Modal.info({
        title:"确认",
        content:"请先选择要排序的分类"
      });
      return;
    }
    tiyData.tData.sortData.sortLevel = level;
    tiyData.status.showSortModal(levelTitle + "排序");
  },
  sortSequence(index1,index2){
    tiyData.tData.changeSort(index1,index2)
  },
  saveSort(){
    webSortDoing(this,tiyData)
  },
  saveMove(){
    webMoveDoing(this,tiyData)
  },
  addRecommend(){
    if(!this.tempRecommand.name || !this.tempRecommand.url){
      this.$Modal.error({content:"关联推荐的名称和url不能为空。"});
      return;
    }
    let recommend = {
      id:"",
      classificationId:tiyData.tParam.classification.id,
      type:2,
      name:this.tempRecommand.name,
      description:"",
      url:this.tempRecommand.url,
      sequence:tiyData.tParam.classification.recommends?tiyData.tParam.classification.recommends.length:0
    }
    tiyData.tParam.classification.recommends.push(recommend);
    this.tempRecommand = {
      name:'',
      url:'',
      decription:''
    }
  },
  removeRecommend(index){
    tiyData.tParam.classification.recommends.splice(index, 1);
  },
  changeState(){
    let classification = tiyData.tParam.classification;
    // if(classification.state === 0 && !classification.shortLinkName){
    //   classification.state = 1;
    //   this.$Modal.info({
    //     title:"确认",
    //     content:"不能启用没有短链接地址的分类"
    //   });
    // }
  },
  toEditStandRoom(){ //跳转到展厅列表
    let path = "/stands/list";
    let param =  {
      standId: this.param.classification.showRoomId,
      classificationId:this.param.classification.id
    }
    this.$router.push({ path: path, query:param});
  },
  isPlaceholderChange(value){
    if(value){
      this.param.classification.type=1;
    }
  },
  queryShowroomList(){
    queryShowroomList(this)
  },
  hideShowroomSelectionModal() {
    this.status.modalShowroom.isShow = false;

  },
  modifyShowroom(){
    modifyShowroom(this,tiyData);
  },
  selectShowroom(){
    this.status.modalShowroom.isShow = true;
  },
  cancelShowroom(){
    this.param.classification.showRoomId='';
    this.param.showroomSelected={
      showroomTitle:'',
      showroomId:'',
      shortLink:''
    }
  }
};

export const VueMounted  = function () {
  init(this, tiyData);
  webList(this, tiyData);
};


